﻿Imports System.Data.SqlClient

Public Class frmStaffIncidents

    Sub FillStaff()
        Try
            Me.Cursor = Cursors.Default

            Dim cmd As New SqlCommand("select EmpNo,FullNameEn from StaffProfiles", cnn)
            Dim Reader As SqlDataReader

            Me.ListUsers.Items.Clear()
            Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListViewItem
                Item.Text = Reader.Item("EmpNo")
                Item.SubItems.Add(Reader.Item("FullNameEn"))
                Me.ListUsers.Items.Add(Item)
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub txtId_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Clear()
    End Sub

    Sub Clear()
        Me.TxtName.Clear()
        Me.txtBranch.Clear()
        Me.txtPosition.Clear()
        Me.dtpIncidentDate.Value = Now
        Me.txtDecisionMade.Clear()
        Me.txtIncidentReason.Clear()
        Me.DataGridView1.Rows.Clear()
    End Sub

    Private Sub butClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butClear.Click
        Me.dtpIncidentDate.Value = Now
        Me.txtDecisionMade.Clear()
        Me.txtIncidentReason.Clear()
    End Sub

    Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butClose.Click
        Me.Close()
    End Sub

    Sub FillIncidents()
        Try
            If Me.txtEmpNo.Text.Trim.Length = 0 Then Exit Sub
            Me.Cursor = Cursors.WaitCursor
            Dim cmd As New SqlCommand
            cmd.CommandText = " Select  * From Incidents Where EmpNo = @EmpNo "
            cmd.Parameters.AddWithValue("@EmpNo", txtEmpNo.Text.Trim)
            cmd.Connection = cnn
            Dim Reader As SqlDataReader
            cnn.Open()
            Reader = cmd.ExecuteReader
            With Me.DataGridView1
                .Rows.Clear()
                While Reader.Read
                    Dim x As Integer = .Rows.Add
                    .Rows(x).Cells(dgvcIncidentDate.Name).Value = CType(Reader("IncidentDate"), DateTime).ToString("dd/MM/yyyy")
                    .Rows(x).Cells(dgvcIncidentReason.Name).Value = Reader("IncidentReason")
                    .Rows(x).Cells(dgvcDecisionMade.Name).Value = Reader("DecisionMade")
                    .Rows(x).Cells(dgvcUserName.Name).Value = Reader("UserName")
                    .Rows(x).Cells(Column1.Name).Value = Reader("TransDate")
                End While
            End With
            cnn.Close()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub


    Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
        If TxtName.Text.Trim.Length = 0 Then
            MsgBox("Please enter a valid staff ID!")
            Exit Sub
        ElseIf txtIncidentReason.Text.Trim.Length = 0 Or txtDecisionMade.Text.Trim.Length = 0 Then
            MsgBox("Please complete the Incident details")
            Exit Sub
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand(" Insert Into Incidents (EmpNo,Name,Position,Branch,IncidentDate,IncidentReason," & _
                                          " DecisionMade,UserId,UserName,TransDate) values(@EmpNo,@Name,@Position,@Branch,@IncidentDate,@IncidentReason," & _
                                          " @DecisionMade,@UserId,@UserName,GetDate())", cnn)
                cmd.Parameters.AddWithValue("@EmpNo", Me.txtEmpNo.Text)
                cmd.Parameters.AddWithValue("@Name", Me.TxtName.Text)
                cmd.Parameters.AddWithValue("@Position", Me.txtPosition.Text)
                cmd.Parameters.AddWithValue("@Branch", Me.txtBranch.Text)
                cmd.Parameters.AddWithValue("@IncidentDate", Me.dtpIncidentDate.Value)
                cmd.Parameters.AddWithValue("@IncidentReason", Me.txtIncidentReason.Text.Trim)
                cmd.Parameters.AddWithValue("@DecisionMade", Me.txtDecisionMade.Text.Trim)
                cmd.Parameters.AddWithValue("@UserId", CurrentUserID)
                cmd.Parameters.AddWithValue("@UserName", CurrentUser)
                cnn.Open()
                cmd.ExecuteNonQuery()
                cnn.Close()

                MsgBox("Saved successfully!")

                Me.txtIncidentReason.Clear()
                Me.txtDecisionMade.Clear()
                Me.dtpIncidentDate.Value = Now
                FillIncidents()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Sub FillEmpData()
        If Me.ListUsers.SelectedItems.Count <= 0 Then
            Exit Sub
        Else

            Try
                Me.Cursor = Cursors.WaitCursor
                Dim cmd As New SqlCommand("Select EmpNo,FullNameEn,Position, Branch From StaffProfiles where EmpNo= @EmpNo", cnn)
                cmd.Parameters.AddWithValue("@EmpNo", Me.ListUsers.SelectedItems(0).Text)
                Dim Reader As SqlDataReader
                Me.TxtName.Clear()
                Me.txtPosition.Clear()
                Me.txtBranch.Clear()
                cnn.Open()
                Reader = cmd.ExecuteReader
                While Reader.Read
                    Me.txtEmpNo.Text = Reader.Item("EmpNo")
                    Me.TxtName.Text = Reader.Item("FullNameEn")
                    Me.txtPosition.Text = Reader.Item("Position")
                    Me.txtBranch.Text = Reader.Item("Branch")

                End While
                cnn.Close()
                FillIncidents()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub txtEmpNo_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            FillEmpData()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim a As New frmSearchEmp
        a.ShowDialog()

        If SelEmpID <> 0 Then
            Me.txtEmpNo.Text = SelEmpID
            FillEmpData()
        End If

    End Sub

    Private Sub txtEmpNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Clear()
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If TxtName.Text.Trim.Length = 0 Then
            MsgBox("Please enter a valid staff ID!")
            Exit Sub
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                ' Set Report DataSet 
                Dim das As New DataSet
                Dim dap As New SqlDataAdapter("Select * From Incidents Where (EmpNo=@EmpNo) ", cnn)
                dap.SelectCommand.Parameters.AddWithValue("@EmpNo", Me.txtEmpNo.Text)


                dap.Fill(das, "Incidents")

                ' Creat Report and Pass The data
                Dim rpt As New StaffIncidents
                rpt.SetDataSource(das)
                rptViewer.CrystalReportViewer1.ReportSource = rpt
                rptViewer.CrystalReportViewer1.RefreshReport()
                rptViewer.ShowDialog()

                txtEmpNo.Focus()
                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub frmStaffIncidents_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillStaff()
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Sub FillUsers()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select EmpNo,FullNameEn From StaffProfiles", cnn)
            Dim Reader As SqlDataReader

            Me.ListUsers.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListViewItem
                Item.Text = Reader.Item("EmpNo")
                Item.SubItems.Add(Reader.Item("FullNameEn"))
                Me.ListUsers.Items.Add(Item)
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
        End Try
    End Sub

    Private Sub ListUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListUsers.SelectedIndexChanged
        Clear()
        FillEmpData()
    End Sub
End Class